package tablet.module_user.activity;

import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.google.android.material.tabs.TabLayout;
import com.lib.app.ARouterPathUtils;
import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.views.circle_image.niv.NiceImageView;
import com.lib.http.call_back.HttpDialogCallBack;
import com.lib.ui.activity.BaseAppActivity;
import com.lib.utls.glide.GlideConfig;
import com.lib.view.navigationbar.NomalNavigationBar;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import tablet.module_user.R;
import tablet.module_user.R2;
import tablet.module_user.bean.TeacherUserBean;
import tablet.module_user.fragment.teacherspace.UserRecordVideoFragment;
import tablet.module_user.fragment.teacherspace.UserTeachResFragment;

@Route(path = ARouterPathUtils.Tablet_User_InfoActivity)
public class UserInfoActivity extends BaseAppActivity {

    @BindView(R2.id.iv_user)
    NiceImageView ivUser;
    @BindView(R2.id.tv_name)
    TextView tvName;
    @BindView(R2.id.tv_nick)
    TextView tvNick;
    @BindView(R2.id.tv_sex)
    TextView tvSex;
    @BindView(R2.id.tv_age)
    TextView tvAge;
    @BindView(R2.id.tv_descripe)
    TextView tvDescripe;
    @BindView(R2.id.tv_teachNo)
    TextView tvTeachNo;
    @BindView(R2.id.tv_school)
    TextView tvSchool;
    @BindView(R2.id.tv_level)
    TextView tvLevel;
    @BindView(R2.id.tv_subject)
    TextView tvSubject;
    @BindView(R2.id.tv_grade)
    TextView tvGrade;
    @BindView(R2.id.tv_city)
    TextView tvCity;
    @BindView(R2.id.tl_tabs)
    TabLayout tlTabs;
    @BindView(R2.id.vp_content)
    ViewPager vpContent;

    @Autowired(name = "teacherId")
    int teacherId;

    List<Fragment> fgList;
    public String[] titleArr = {"录播课程", "教辅资料"};

    UserRecordVideoFragment videoFragment;
    UserTeachResFragment teachResFragment;

    @Override
    protected void onCreateView() {

        initTitle();

        teacherId = getIntent().getIntExtra("teacherId", -1);

        initViewPager();
        initTabNormal();

        //初始化用户数据
        initUserInfoData();



    }

    protected void initTitle() {

        NomalNavigationBar navigationBar = new
                NomalNavigationBar.Builder(this)
                .setTitle("老师空间")
                .builder();

    }


    private void initUserInfoData() {

        HttpUtils.with(this)
                .addParam("requestType", "TABLET_PROFILE")
                .addParam("token", SharedPreferenceManager.getInstance(this).getUserCache().getUserToken())
                .addParam("teacher_id", teacherId)
                .execute(new HttpDialogCallBack<TeacherUserBean>() {
                    @Override
                    public void onSuccess(TeacherUserBean result) {

                        if (CodeUtil.CODE_200 == result.getCode()) {
                            if (result.getObj() != null) {

                                initUserInfo(result.getObj());
                            }
                        }
                    }

                    @Override
                    public void onError(String e) {

                    }
                });

    }

    private void initUserInfo(TeacherUserBean.ObjBean obj) {

        if (StringUtil.isNotEmpty(obj.getPhoto_url(), true)) {
            Glide.with(this)
                    .load(obj.getPhoto_url())
                    .apply(GlideConfig.getCircleOptions())
                    .into(ivUser);
        }

        if (StringUtil.isNotEmpty(obj.getUser_name(), true)) {
            tvName.setText(obj.getUser_name());
        }
        if (StringUtil.isNotEmpty(obj.getUser_name(), true)) {
            tvNick.setText(obj.getUser_name());
        }
        if (StringUtil.isNotEmpty(obj.getSex(), true)) {
            if ("0".equals(obj.getSex())) {
                tvSex.setText("男");
            } else if ("1".equals(obj.getSex())) {
                tvSex.setText("女");
            }
        }
        if (StringUtil.isNotEmpty(obj.getWork_year() + "", true)) {
            tvAge.setText(obj.getWork_year() + "年");
        }
        if (StringUtil.isNotEmpty(obj.getIntroduction(), true)) {
            tvDescripe.setText(obj.getIntroduction());
        }
        if (StringUtil.isNotEmpty(obj.getCertificate_number(), true)) {
            tvTeachNo.setText(obj.getCertificate_number());
        }
        if (StringUtil.isNotEmpty(obj.getSchool_name(), true)) {
            tvSchool.setText(obj.getSchool_name());
        }
        if (StringUtil.isNotEmpty(obj.getJob_name(), true)) {
            tvLevel.setText(obj.getJob_name());
        }
        if (StringUtil.isNotEmpty(obj.getSubject_name(), true)) {
            tvSubject.setText(obj.getSubject_name());
        }
        if (StringUtil.isNotEmpty(obj.getGrade_name(), true)) {
            tvGrade.setText(obj.getGrade_name());
        }
        if (StringUtil.isNotEmpty(obj.getArea(), true)) {
            tvCity.setText(obj.getArea());
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_userinfo;
    }


    /**
     * 初始化ViewPager，方便后期与tabLayout关联
     */
    private void initViewPager() {
        if (fgList == null)
            fgList = new ArrayList<>();

        if (teacherId > 0) {
            videoFragment = new UserRecordVideoFragment(teacherId);
            teachResFragment = new UserTeachResFragment(teacherId);
        }

        addFragmentToList();

        ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fgList);
        vpContent.setAdapter(pagerAdapter);
        pagerAdapter.notifyDataSetChanged();
    }

    public void addFragmentToList() {

        if (fgList.size() > 0)
            fgList.clear();

        fgList.add(videoFragment);
        fgList.add(teachResFragment);
    }

    /**
     * 初始化顶部标签
     */
    private void initTabNormal() {
        tlTabs.setupWithViewPager(vpContent);
        tlTabs.setSelectedTabIndicator(R.drawable.tablayout_indicator);

        tlTabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //添加选中Tab的逻辑
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //添加未选中Tab的逻辑
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                //再次选中tab的逻辑
            }
        });

    }


    /**
     * ViewPager的适配器。
     */
    class ViewPagerAdapter extends FragmentPagerAdapter {

        List<Fragment> fragmentList;

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragmentList) {
            super(fm);
            this.fragmentList = fragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titleArr[position];
        }
    }
}
